package com.lmonkey.servlet.cart;

import com.lmonkey.servic.CARTDao;
import com.lmonkey.servic.CATAGORYDao;
import com.lmonkey.shop.CART;
import com.lmonkey.shop.CATEGORY;
import com.lmonkey.shop.PRODUCT;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/shangc/showcart")
public class ShowCart extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //菜单
        ArrayList<CATEGORY> flist = CATAGORYDao.selectcat("father");
        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> clist = CATAGORYDao.selectcat("child");
        request.setAttribute("clist",clist);

        HttpSession session = request.getSession();
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");
        if (user != null && isLogin.equals("1")){
            String uid = (String)user.getuser_id();

            ArrayList<CART> list = CARTDao.getcart(uid);
            request.setAttribute("shoplist",list);
            request.getRequestDispatcher("cart.jsp").forward(request,response);
        }else {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请前往登录');location.href='login.jsp';</script>");
            out.close();
            return;
        }
    }
}
